import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as Global from '../TS/Global';
import * as Mycss from "../CSS/mycss.css";
import * as MyCfg from './MyCfg';
import * as Loading from './Loading';
import {MessageBox} from './messagebox';
export interface ModelProps extends Global.Myprops {
    ttxx?: string
};

interface ModelState {};

export class Model extends React.Component < ModelProps,
ModelState > {
    constructor(props) {
        super(props);
        //设置状态
        this.state = {
            // secondsElapsed: 0
        };

    }
    GetWeb() : void
    {
        console.log("执行命令");
        let promise = new Promise(function (resolve, reject) {

            $.ajax({
                url: "WebService.cgi",

                type: 'post',
                error: (xhr, status, error) => {

                    reject(error);
                },
                contentType: "application/x-www-form-urlencoded",
                data: {
                    cmd: 'GetCfg'
                },
                dataType: "json",
                // dataType: "jsonp", jsonp: "callback", jsonpCallback: "success_jsonpCallback",
                success: (result) => {
                    resolve(result);
                }
            });
        });
        promise.then((results : any) => {
            console.log("获取数据成功");
            let control : any = this.refs.loading;
            control.ReciveMsgEvent(this, "close", "");
            //this.forceUpdate();
            console.log(results)
        }).catch((errors) => {

            let control : any = this.refs.loading;
            control.ReciveMsgEvent(this, "close", "");
            console.log("获取数据失败");
            let control_msg : any = this.refs.messagebox;
            control_msg.ShowError("获取配置信息出错:" + errors);
        });
    }
    //用于处理收到的数据
    ReciveMsgEvent(source : any, cmd : string, obj?: any, ...args) {
        if (cmd == 'update') {
            this.forceUpdate();
        } else {}
    }

    //下一步进行显示 进行初始化
    componentDidMount() {
        //类似window.onload
        this.setState((prv, prop) => ({
            // count: this.state.count + 1
        }));
    }
    componentWillReceiveProps(nextProps)
    {
        //每次父控件重新绘制,赋值新的props触发
        this.setState({});
    }
    //处理点击
    public handleClick(e : React.MouseEvent < any >) : void {}
    //处理页面变换
    public handleChange(event : any) : void {
        let tcfg: any = {};
        let value: any;
        let isset: boolean = false;
        if (event.target.type == "checkbox") {

            tcfg[event.target.name] = event.target.checked;
            isset = true;
        } else if (event.target.type == "radio") {
            if (event.target.checked) {

                tcfg[event.target.name] = event.target.value;
                isset = true;
            }

        } else if (event.target.type == "select-one") {
            // <option value='0'>全部</option>
            var index = event.target.selectedIndex;
            value = event.target.options[index].value;

        } else {
            value = event.target.value;
        }
        if (!isset) {

            tcfg[event.target.name] = value;
        }
        this.setState(tcfg, () => {});
    }
    //处理按键按下事件
    public handleKeyDown(event : React.KeyboardEvent < HTMLInputElement >) : void {
        if(event.keyCode == 13) {
            //处理回车事件
        }
    }
    //进行渲染
    public render() : JSX.Element {return(
            <div className="col-xs-12 col-sm-10">
                <div className="panel panel-default">
                    <div className="panel-heading">
                        <h3 className="panel-title">xxx设置</h3>
                    </div>
                    <div className="panel-body"></div>

                    <MessageBox ref="messagebox" MessageType='alert' Title="提醒" Content="成功"/>
                    <Loading.Loading ref="loading" ShowText="加载中..." IsShow={false}/>
                </div>
            </div>
        );}
}